from django.db import models
from libs.db import BaseModel


class Entity(BaseModel):
    key = models.CharField('实体标识符', max_length=100, unique=True)
    name = models.CharField('名称', max_length=100, unique=True)
    source_db = models.CharField('数据库来源', max_length=100, null=True, blank=True)
    source_table = models.CharField('表来源', max_length=100, null=True, blank=True)
    source_updated_at_field = models.CharField('更新时间字段', max_length=100, null=True, blank=True)
    primary_key = models.CharField('主键字段', max_length=100)
    sql_filter = models.CharField('过滤条件，用于排除一些无效的数据', max_length=300, null=True, blank=True)
    standard_model = models.CharField('标准模型', max_length=100, null=True, blank=True)

    class Meta:
        verbose_name = '实体'
        verbose_name_plural = '实体'
        ordering = ('-id',)


class EntityField(BaseModel):
    entity = models.ForeignKey(Entity, related_name='fields', on_delete=models.CASCADE)
    field = models.CharField('字段', max_length=100)
    label = models.CharField('名称', max_length=100)
    source_field = models.CharField('来源于表中哪个字段', max_length=100, null=True, blank=True)
    standard_model_field = models.CharField('对应的标准模型字段', max_length=100, null=True, blank=True)

    class Meta:
        verbose_name = '实体属性'
        verbose_name_plural = '实体属性'
        ordering = ('-id',)
        unique_together = ['entity', 'field']
